﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp4
{
    public partial class Form1 : Form
    {
        MySqlConnection conn;
        MySqlDataAdapter mda;
        DataSet ds;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void print_in_dataGridView()
        {
            MySqlCommand mycom = conn.CreateCommand();
            mycom.CommandText = "SELECT * FROM student ; ";
            MySqlDataAdapter adap = new MySqlDataAdapter(mycom);
            DataSet ds = new DataSet();
            adap.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0].DefaultView;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            string M_str_sqlcon = "server=localhost;user id=root;password=20010401;database=test";                                                                                              //创建数据库连接对象
            conn = new MySqlConnection(M_str_sqlcon);
            try
            {
                //打开数据库连接
                conn.Open();
                MessageBox.Show("数据库已经连接了！");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            print_in_dataGridView();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            string table_name = textBox1.Text.Trim();
            string name = textBox3.Text.Trim();
            string date = dateTimePicker1.Text;
            string sex = textBox4.Text.Trim();
            if (table_name == null && (table_name != "教师") && (table_name!="学生"))

            {
                MessageBox.Show("请正确填写表格名");
            }

            else
            {
                
                string sql = "select * from " + table_name;
                mda = new MySqlDataAdapter(sql, conn);
                ds = new DataSet();
                mda.Fill(ds, table_name);
                //显示数据
                dataGridView1.DataSource = ds.Tables[table_name];
                conn.Close();

            }

        }

        private void button4_Click(object sender, EventArgs e)
        {
            String table_name = textBox1.Text.Trim();
            String Id = textBox2.Text.Trim();
            String Name = textBox3.Text.Trim();
            String Sex = textBox4.Text.Trim();
            String Birth = dateTimePicker1.Value.ToString("yyyy-MM-dd");
            string[] row = { Id, Name, Birth, Sex };

            try
            {
                conn.Open();
                String insertstr = "INSERT INTO Student (s_id,s_name,s_birth,s_sex) VALUES" + "(" + Id + "," + Name + "," + Birth + "," + Sex + ");";
                MySqlCommand cmd = new MySqlCommand(insertstr, conn); //实例化数据库命令对象
                cmd.ExecuteNonQuery(); //执行命令
            }
            catch { MessageBox.Show("输入数据有误，请输入有效数据！", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); }
            finally { conn.Close(); }
            //显示到dataGridView
            print_in_dataGridView();
        }

      


        

        private void button2_Click(object sender, EventArgs e)
        {
            int flag1 = 0, flag2 = 0;
            try
            {
                conn.Open();//打开数据库
                string updatestr = "UPDATE Student SET ";
                String table_name = textBox1.Text.Trim();
                String Id = textBox2.Text.Trim();
                String Name = textBox3.Text.Trim();
                String Sex = textBox4.Text.Trim();
                String Birth = dateTimePicker1.Value.ToString("yyyy-MM-dd");
                string[] row = { Id, Name, Birth, Sex };
                
                if (checkBox_birth.Checked == true)
                {
                    if (flag1 == 0)
                    {
                        updatestr += "s_birth = " + "\"" + Birth + "\"";

                        flag1 = 1;

                    }
                    else
                        updatestr += ", s_birth = " + "\"" + Birth + "\"";
                }
                if (checkBox_name.Checked == true)
                {
                    if (flag1 == 0)
                    {
                        updatestr += "s_name = " + "\"" + Name + "\"";
                        flag1 = 1;
                    }
                    else
                        updatestr += ", s_name = " + "\"" + Name + "\"";
                }
                if (checkBox_sex.Checked == true)
                {
                    if (flag1 == 0)
                    {
                        updatestr += "s_sex = " + "\"" + Sex + "\"";
                        flag1 = 1;
                    }
                    else
                        updatestr += ", s_sex = " + "\"" + Sex + "\"";
                }
                updatestr += " WHERE s_id = " + "\"" + Id + "\"";
                MySqlCommand cmd = new MySqlCommand(updatestr, conn);
                cmd.ExecuteNonQuery();
            }
            catch
            {
                flag2 = 1;
                MessageBox.Show("输入数据违反要求!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally { conn.Close(); }

            print_in_dataGridView();
            if (flag2 == 0)
            {
                MessageBox.Show("修改成功！", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
        try
                    {
                        conn.Open();
                        string select_id = textBox2.Text;//选择的当前行第一列的值，也就是ID
                        string delete_by_id = "delete from Student where s_id = " + "\"" + select_id + "\"";//sql删除语句,根据学号删除
                        MySqlCommand cmd = new MySqlCommand(delete_by_id, conn);
                        cmd.ExecuteNonQuery(); //执行命令
                    }
                    catch { MessageBox.Show("请正确选择行!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); }
                    finally { conn.Dispose(); }
                    print_in_dataGridView();
        }
    }
}
